package com.mall.order.biz.handler;

import com.mall.commons.tool.exception.BizException;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.user.IMemberService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.QueryMemberRequest;
import com.mall.user.dto.QueryMemberResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 校验处理
 * @author Javid Xi
 * @version 1.0
 * @since 2021/7/20
 */
@Slf4j
@Component
public class ValidateHandler extends AbstractTransHandler {

    @Reference(check = false)
    private IMemberService memberService;

    /**
     * 验证用户合法性
     * @return boolean
     */
    @Override
    public boolean isAsync() {
        return false;
    }

    /**
     * 验证用户合法性
     * @return boolean
     */
    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext createOrderContext = (CreateOrderContext) context;
        QueryMemberRequest queryRequest = new QueryMemberRequest();
        queryRequest.setUserId(createOrderContext.getUserId());

        QueryMemberResponse memberResponse = memberService.queryMemberById(queryRequest);
        if(!memberResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            throw new BizException(OrderRetCode.SYSTEM_ERROR.getCode(),"用户信息校验失败");
        }
        return true;
    }
}
